package com.nononsenseapps.feeder.ui.tag

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.gitee.wsl.struct.category.CategoryItemWarp
import cn.ekuma.odbo.mobile.category.base.CategoryItemWarpDiff

import com.nononsenseapps.feeder.ui.R
import timber.log.Timber

class ItemTagAdapter(val context: Context, private val itemTagCallBack:ItemTagClickCallBack,val repository: ItemTagRepository):  ListAdapter<CategoryItemWarp, RecyclerView.ViewHolder>(
    CategoryItemWarpDiff()
)   {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ItemTagHolder(LayoutInflater.from(parent.context).inflate(R.layout.list_item_tag, parent, false),itemTagCallBack, repository)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int,payloads: MutableList<Any?>) {
       if (payloads.isNotEmpty()) {
            for (payload in payloads) {
                if (payload is CategoryItemWarpDiff.CategoryItemTags) {
                    getItem(position).isExpand =payload.showAll
                }
            }
        }
        super.onBindViewHolder(holder, position, payloads)
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val wrap = getItem(position)
        val itemTagHolder=holder as ItemTagHolder
        itemTagHolder.updateItem(wrap as CategoryItemWarp)
    }

    fun submitFeedItemTagList(rootTag: CategoryItemWarp) {
        val showItemList =  mutableListOf<CategoryItemWarp>()

        val rootLeafItem=CategoryItemWarp(rootTag.data!!)

        val leafList=rootTag.leafList()
        rootLeafItem.addSubtrees(leafList)

        showItemList.add(rootLeafItem)

        val categoryList=rootTag.categoryItemList().map { it as CategoryItemWarp }.sortedBy { item->
            repository.getAttentionLevel(item)
        }

        showItemList.addAll(categoryList)

        Timber.d("call in ItemTagsFilterDrawerPopup launch InItem count:%d, item count:,root item count:%d,root leaf count:%d,root category count:%d",showItemList.size,rootTag.subItemList.size,leafList.size,categoryList.size)

        super.submitList(showItemList)
    }

}

interface ItemTagClickCallBack{
    fun selectedTagsToFilter(tags:List<String>)

    fun onItemTagClick(itemTagWap:CategoryItemWarp)

    fun onItemTagLongClick(itemTagWap: CategoryItemWarp, view: View)
}

abstract class AbstractItemTagClickCallBack:ItemTagClickCallBack{
    override fun selectedTagsToFilter(tags: List<String>) {
    }

    override fun onItemTagClick(itemTagWap: CategoryItemWarp) {
    }

    override fun onItemTagLongClick(itemTagWap: CategoryItemWarp, view: View) {
    }
}

open class ItemTagClickCallBackAdapter(val callBack: ItemTagClickCallBack) :ItemTagClickCallBack{
    override fun selectedTagsToFilter(tags: List<String>) {
        callBack.selectedTagsToFilter(tags)
    }

    override fun onItemTagClick(itemTagWap: CategoryItemWarp) {
        callBack.onItemTagClick(itemTagWap)
    }

    override fun onItemTagLongClick(itemTagWap: CategoryItemWarp, view: View) {
        callBack.onItemTagLongClick(itemTagWap,view)
    }
}
